package gentle.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import gentle.base.SqlMapper;
import gentle.base.StringUtil;
import gentle.entity.ConsultantInfo;
import gentle.entity.CustomerAppointmentInfo;
import gentle.entity.SearchCustomerAppointmentInfo;
import gentle.mapper.CustomerAppointmentDao;
import gentle.service.ICustomerAppointmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CustomerAppointmentServiceImpl implements ICustomerAppointmentService {

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    private CustomerAppointmentDao dao;

    @Override
    public List<CustomerAppointmentInfo> selectPageListByQueryGetMap(Page<CustomerAppointmentInfo> page, SearchCustomerAppointmentInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTreatmentUnit())) {
            params.put("treatmentUnit", searchInfo.getTreatmentUnit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
            if (searchInfo.getOneChannel().indexOf("请选择") == -1) {
                String oneChannel = "";
                String oneChannel2[] = searchInfo.getOneChannel().split(",");
                if (oneChannel2.length == 1) {
                    oneChannel = oneChannel2[0];
                } else {
                    for (int i = 0; i < oneChannel2.length; i++) {
                        if (i == (oneChannel2.length - 1)) {
                            oneChannel += "'" + oneChannel2[i];
                        } else if (i == 0) {
                            oneChannel += oneChannel2[i] + "'" + ",";
                        } else {
                            oneChannel += "'" + oneChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("oneChannel", " t.one_channel in ('" + oneChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
            if (searchInfo.getTwoChannel().indexOf("请选择") == -1) {
                String twoChannel = "";
                String twoChannel2[] = searchInfo.getTwoChannel().split(",");
                if (twoChannel2.length == 1) {
                    twoChannel = twoChannel2[0];
                } else {
                    for (int i = 0; i < twoChannel2.length; i++) {
                        if (i == (twoChannel2.length - 1)) {
                            twoChannel += "'" + twoChannel2[i];
                        } else if (i == 0) {
                            twoChannel += twoChannel2[i] + "'" + ",";
                        } else {
                            twoChannel += "'" + twoChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("twoChannel", " t.two_channel in ('" + twoChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
            if (searchInfo.getThrChannel().indexOf("请选择") == -1) {
                String thrChannel = "";
                String thrChannel2[] = searchInfo.getThrChannel().split(",");
                if (thrChannel2.length == 1) {
                    thrChannel = thrChannel2[0];
                } else {
                    for (int i = 0; i < thrChannel2.length; i++) {
                        if (i == (thrChannel2.length - 1)) {
                            thrChannel += "'" + thrChannel2[i];
                        } else if (i == 0) {
                            thrChannel += thrChannel2[i] + "'" + ",";
                        } else {
                            thrChannel += "'" + thrChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("thrChannel", " t.thr_channel in ('" + thrChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            if("1".equals(searchInfo.getCustomerStatus())){
                params.put("appointmentStatus"," and (arrival_status=1 or arrival_status=6) ");
            } else {
                params.put("appointmentStatus"," and (arrival_status=9 or arrival_status=10) ");
            }
        }
        params.put("tableName", tableHead +"customer_info");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPage(page, params);
    }

    public int getQueryPagesCount(Page<CustomerAppointmentInfo> page, SearchCustomerAppointmentInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getSreach())) {
            params.put("sreach", searchInfo.getSreach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtil.isNotEmpty(searchInfo.getTreatmentUnit())) {
            params.put("treatmentUnit", searchInfo.getTreatmentUnit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            if("1".equals(searchInfo.getCustomerStatus())){
                params.put("appointmentStatus"," and (arrival_status=1 or arrival_status=6) ");
            } else {
                params.put("appointmentStatus"," and (arrival_status=9 or arrival_status=10) ");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
            if (searchInfo.getOneChannel().indexOf("请选择") == -1) {
                String oneChannel = "";
                String oneChannel2[] = searchInfo.getOneChannel().split(",");
                if (oneChannel2.length == 1) {
                    oneChannel = oneChannel2[0];
                } else {
                    for (int i = 0; i < oneChannel2.length; i++) {
                        if (i == (oneChannel2.length - 1)) {
                            oneChannel += "'" + oneChannel2[i];
                        } else if (i == 0) {
                            oneChannel += oneChannel2[i] + "'" + ",";
                        } else {
                            oneChannel += "'" + oneChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("oneChannel", " t.one_channel in ('" + oneChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
            if (searchInfo.getTwoChannel().indexOf("请选择") == -1) {
                String twoChannel = "";
                String twoChannel2[] = searchInfo.getTwoChannel().split(",");
                if (twoChannel2.length == 1) {
                    twoChannel = twoChannel2[0];
                } else {
                    for (int i = 0; i < twoChannel2.length; i++) {
                        if (i == (twoChannel2.length - 1)) {
                            twoChannel += "'" + twoChannel2[i];
                        } else if (i == 0) {
                            twoChannel += twoChannel2[i] + "'" + ",";
                        } else {
                            twoChannel += "'" + twoChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("twoChannel", " t.two_channel in ('" + twoChannel + "')");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
            if (searchInfo.getThrChannel().indexOf("请选择") == -1) {
                String thrChannel = "";
                String thrChannel2[] = searchInfo.getThrChannel().split(",");
                if (thrChannel2.length == 1) {
                    thrChannel = thrChannel2[0];
                } else {
                    for (int i = 0; i < thrChannel2.length; i++) {
                        if (i == (thrChannel2.length - 1)) {
                            thrChannel += "'" + thrChannel2[i];
                        } else if (i == 0) {
                            thrChannel += thrChannel2[i] + "'" + ",";
                        } else {
                            thrChannel += "'" + thrChannel2[i] + "'" + ",";
                        }
                    }
                }
                params.put("thrChannel", " t.thr_channel in ('" + thrChannel + "')");
            }
        }
        params.put("tableName", tableHead +"customer_info");
        return dao.getQueryPagesCount(page, params);
    }

    public List<ConsultantInfo> changeTotal(String startTime, String endTime, String oneChannelValue, String doctor,
                                     String twoChannelValue, String thrChannelValue, String fieldTc, String treatmentUnit,
                                     String sreach , String abbreviation){

        String ctable = abbreviation + "customer_info";
        String contitionInfo = "";
        if (StringUtils.isNotBlank(startTime)) {
            contitionInfo += " and t.start_time >= '" + startTime + "' ";
        }
        if (StringUtils.isNotBlank(endTime)) {
            contitionInfo += " and t.start_time <= '" + endTime + "' ";
        }
        if (StringUtils.isNotBlank(fieldTc)) {
            contitionInfo += " and t.field_tc = '" + fieldTc + "' ";
        }
        if (StringUtils.isNotBlank(doctor)) {
            contitionInfo += " and t.doctor = '" + doctor + "' ";
        }
        if (StringUtils.isNotBlank(treatmentUnit)) {
            contitionInfo += " and t.treatment_unit = '" + treatmentUnit + "' ";
        }
        if (StringUtil.isNotEmpty(oneChannelValue)) {
            if (oneChannelValue.indexOf("请选择") == -1) {
                String oneChannel = "";
                String oneChannel2[] = oneChannelValue.split(",");
                if (oneChannel2.length == 1) {
                    oneChannel = oneChannel2[0];
                } else {
                    for (int i = 0; i < oneChannel2.length; i++) {
                        if (i == (oneChannel2.length - 1)) {
                            oneChannel += "'" + oneChannel2[i];
                        } else if (i == 0) {
                            oneChannel += oneChannel2[i] + "'" + ",";
                        } else {
                            oneChannel += "'" + oneChannel2[i] + "'" + ",";
                        }
                    }
                }
                contitionInfo += " and t.one_channel in ( '" + oneChannel + "') ";
            }
        }
        if (StringUtil.isNotEmpty(twoChannelValue)) {
            if (twoChannelValue.indexOf("请选择") == -1) {
                String twoChannel = "";
                String twoChannel2[] = twoChannelValue.split(",");
                if (twoChannel2.length == 1) {
                    twoChannel = twoChannel2[0];
                } else {
                    for (int i = 0; i < twoChannel2.length; i++) {
                        if (i == (twoChannel2.length - 1)) {
                            twoChannel += "'" + twoChannel2[i];
                        } else if (i == 0) {
                            twoChannel += twoChannel2[i] + "'" + ",";
                        } else {
                            twoChannel += "'" + twoChannel2[i] + "'" + ",";
                        }
                    }
                }
                contitionInfo += " and t.two_channel  in ('" + twoChannel + "') ";
            }
        }
        if (StringUtil.isNotEmpty(thrChannelValue)) {
            if (thrChannelValue.indexOf("请选择") == -1) {
                String thrChannel = "";
                String thrChannel2[] = thrChannelValue.split(",");
                if (thrChannel2.length == 1) {
                    thrChannel = thrChannel2[0];
                } else {
                    for (int i = 0; i < thrChannel2.length; i++) {
                        if (i == (thrChannel2.length - 1)) {
                            thrChannel += "'" + thrChannel2[i];
                        } else if (i == 0) {
                            thrChannel += thrChannel2[i] + "'" + ",";
                        } else {
                            thrChannel += "'" + thrChannel2[i] + "'" + ",";
                        }
                    }
                }
                contitionInfo += " and t.thr_channel in ('" + thrChannel + "') ";
            }
        }
        if (StringUtils.isNotBlank(sreach)) {
            contitionInfo += " and (t.customer_name like CONCAT('%','" + sreach + "','%') or t.customer_phone like CONCAT('%','" + sreach + "','%') or t.customer_phone2 like CONCAT('%','" + sreach + "','%') or" +
                    " t.wechat_number like CONCAT('%','" + sreach + "','%') or t.medical_record_number like CONCAT('%','" + sreach + "','%') or t.szm like CONCAT('%" + sreach + "%')) ";
        }       //总人数
        String sql = "select * from (select count(1) as countNum from " + ctable + " t  where 1=1 and (t.arrival_status=1 or t.arrival_status=6 or t.arrival_status=9 or t.arrival_status=10) " + contitionInfo +
                //复诊预约人数
                " union all " +
                "select count(1) as countNum from " + ctable + " t  where 1=1 and (t.arrival_status=1 or t.arrival_status=6) " + contitionInfo +
                //新诊预约人数
                " union all " +
                "select count(1) as countNum from " + ctable + " t  where 1=1 and (t.arrival_status=9 or t.arrival_status=10) " + contitionInfo +
                " ) t" ;
        List<ConsultantInfo> infoList = sqlMapper.selectList(sql, ConsultantInfo.class);
        return infoList;
    }
}